from django import template
from django.template import Node, NodeList, Template, Context, Variable, VariableDoesNotExist
from django.template.defaulttags import IfEqualNode
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.template.loader import render_to_string

import django_widgets.template_helpers as template_helpers
import comments.forms as forms

register = template.Library()

@register.tag(name="compact-comments")
def render_compact_comments(parser,  token):
    try:
        tag,  content_type,  node_id = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError,  "%r tag requires two arguments" % token.contents.split()[0]
    return CompactComments(content_type,  node_id)

@register.tag(name = "standard-comments")
def render_standard_comments(parse,  token):
    try:
        tag,  content_type,  node_id = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError,  "%r tag requires two arguments" % token.contents.split()[0]
    return StandardComments(content_type,  node_id)

class CompactComments(template.Node):
    def __init__(self, content_type,  node_id):
        self.content_type = content_type
        self.node_id = node_id
        
    def render(self,  context):
        content_type = template_helpers.resolve_variable(self.content_type,  context)
        node_id = template_helpers.resolve_variable(self.node_id,  context)
        content_type_object = ContentType.objects.get(id = content_type)
        node = content_type_object.model_class().objects.get(id = node_id)
        form_id = "comment_form_%s_%s" % (content_type,  node_id)
        comment_list_id = "comment_list_%s_%s" % (content_type,  node_id)
        comment_form = forms.CompactCommentForm()
        compound_id = "%s_%s" % (content_type,  node_id)
        
        template = render_to_string('comments/compact_comments.html',   
                                                {'content_type':content_type,  'node_id':node_id,  'node':node,  'form_id':form_id, 
                                                    'comment_form':comment_form,  'comment_list_id':comment_list_id,  'compound_id':compound_id},  context)
        return template

class StandardComments(template.Node):
    def __init__(self, link_id,  box_id,  prefix = None):
        self.link_id = link_id
        self.box_id = box_id
        self.prefix = prefix
    def render(self,  context):
        try:
            link_id = Variable(self.link_id)
            link_id = link_id.resolve(context)
        except VariableDoesNotExist:
            link_id = self.link_id
        if self.prefix:
            link_id = str(self.prefix) + str(link_id)
        ajax_script = "<script type='text/javascript'>" + \
                                    '$("#' + str(self.link_id) + \
                                    '").bind("click", function() {show_overlay("' + str(self.link_id) + '", "' + str(self.box_id) + '"); return false;});' +\
                                "</script>"
        return_value = "\n" + ajax_script + "\n"
        return return_value
